package com.shuqi.activity.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.database.model.BookInfo;

/**
 * 浏览历史
 * 
 * @author Zhuchen
 * @date 2013-12-31
 * @version 1.0
 * 
 */
public class BrowsingHistoryAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<BookInfo> list;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public BrowsingHistoryAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
        this.inflater = LayoutInflater.from(context);
    }

    public void setData(List<BookInfo> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        } else {
            return list.size();
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = null;
        Holder holder = null;
        if (convertView != null) {
            view = convertView;
            holder = (Holder) view.getTag();
        } else {
            view = inflater.inflate(R.layout.item_browsinghistory, parent, false);

            holder = (Holder) view.getTag();
            if (holder == null) {
                holder = new Holder();
                holder.browsinghistory_title_image =
                        (ImageView) view.findViewById(R.id.browsinghistory_title_image);
                holder.browsinghistory_bookname_text =
                        (TextView) view.findViewById(R.id.browsinghistory_bookname_text);
                holder.browsinghistory_type_text =
                        (TextView) view.findViewById(R.id.browsinghistory_type_text);
                holder.browsinghistory_date_text =
                        (TextView) view.findViewById(R.id.browsinghistory_date_text);
                view.setTag(holder);
            }
        }
        BookInfo bookInfo = list.get(position);
        if (bookInfo != null) {
            holder.browsinghistory_bookname_text.setText(list.get(position).getBookName());
            holder.browsinghistory_date_text.setText(format.format(list.get(position).getBookUpdateTime()));
            // +list.get(position).getInfoUpdateTime()
            holder.browsinghistory_type_text.setText(list.get(position).getBookType());
            SqImageManager.getImageManager().displayImage(bookInfo.getBookCoverImgUrl(),
                    holder.browsinghistory_title_image, SqImageManager.IMG_COVER);
        }
        return view;
    }

    private static class Holder {
        public ImageView browsinghistory_title_image;
        public TextView browsinghistory_bookname_text;
        public TextView browsinghistory_type_text;
        public TextView browsinghistory_date_text; // 时间
    }

}
